<html>
<head>
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="canonical" href="./Multiplexer_One_Hot.html">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Takes in a one-hot bit vector (`selector`), and outputs the value of the corresponding input. All selectors and inputs are concatenated into a single vector each.">
<title>Multiplexer One Hot</title>
</head>
<body>

<p class="inline bordered"><b><a href="./Multiplexer_One_Hot.v">Source</a></b></p>
<p class="inline bordered"><b><a href="./legal.html">License</a></b></p>
<p class="inline bordered"><b><a href="./index.html">Index</a></b></p>

<h1>Multiplexer (One-Hot)</h1>
<p>Takes in a one-hot bit vector (<code>selector</code>), and outputs the value of the
 corresponding input. All selectors and inputs are concatenated into
 a single vector each.</p>
<p>If more than one <code>selector</code> bit is set, the output is a Boolean combination
 of the selected input words, as specified by <code>OPERATION</code>: OR for a plain
 multiplexer, AND to show each bit where all the words agree, NOR to show each
 bit which are not set in all the words, etc...</p>
<p>The <code>IMPLEMENTATION</code> parameter defaults to "AND" since this is all Boolean
 logic, though if you are feeding the input words from registers all sharing
 common control logic, you might get a possibly better result by selecting
 "MUX". If it matters for speed or area, use the implementation which ends
 up using the register's reset/clear pin, which will remove the Annuller
 logic from the multiplexer itself.</p>

<pre>
`default_nettype none

module <a href="./Multiplexer_One_Hot.html">Multiplexer_One_Hot</a>
#(
    parameter       WORD_WIDTH          = 0,
    parameter       WORD_COUNT          = 0,
    parameter       OPERATION           = "OR",
    parameter       IMPLEMENTATION      = "AND",

    // Do not set at instantiation
    parameter   TOTAL_WIDTH = WORD_COUNT * WORD_WIDTH
)
(
    input   wire    [WORD_COUNT-1:0]    selectors,
    input   wire    [TOTAL_WIDTH-1:0]   words_in,
    output  wire    [WORD_WIDTH-1:0]    word_out
);
</pre>

<p>First, create a copy of <code>words_in</code>, but with non-selected words annulled.</p>

<pre>
    wire [TOTAL_WIDTH-1:0] words_in_selected;

    generate

        genvar i;

        for (i=0; i < WORD_COUNT; i=i+1) begin : per_word

            <a href="./Annuller.html">Annuller</a>
            #(
                .WORD_WIDTH     (WORD_WIDTH),
                .IMPLEMENTATION (IMPLEMENTATION)
            )
            select_input
            (
                .annul       (selectors[i] == 1'b0),
                .data_in     (words_in          [WORD_WIDTH*i +: WORD_WIDTH]),
                .data_out    (words_in_selected [WORD_WIDTH*i +: WORD_WIDTH])
            );

        end

    endgenerate
</pre>

<p>Then reduce the filtered words to one word, maybe getting a final useful
 computation done in the process, depending on the selected <code>OPERATION</code>.</p>

<pre>
    <a href="./Word_Reducer.html">Word_Reducer</a>
    #(
        .OPERATION  (OPERATION),
        .WORD_WIDTH (WORD_WIDTH),
        .WORD_COUNT (WORD_COUNT)
    )
    combine_words
    (
        .words_in   (words_in_selected),
        .word_out   (word_out)
    );

endmodule
</pre>

<hr>
<p><a href="./index.html">Back to FPGA Design Elements</a>
<center><a href="https://fpgacpu.ca/">fpgacpu.ca</a></center>
</body>
</html>

